$(function() {
	var n = sessionStorage.getItem("new"); //获取用户是否为新用户
	var app = new Vue({
		el: '#app',
		data: {
			Language: {}, //语言包
			content: { //店铺的信息(logo)
				result: {
					shop_list: ["shop_name"]
				}
			},
			mycontent: { //优惠券详细信息(二维码,适用门店等)
				result: {

				}
			},
			shop: {
				shop_logo: "",
				shop_name: "",
				shop_endtime: "",
				list: "", //适用门店
				name: "", //优惠券名字
				con: ""
			} //商家详细信息
			,
			num: sessionStorage.getItem("my_coupon_count"), //自己持有卡券的个数
			sn: "",
			coupon_id:""
		},
		methods: {
			Langes: function(v) {
				$.ajax({
					type: "get",
					dataType: "json",
					url: "../Langue/" + v + ".json",
					success: function(h) {
						app.Language = h;
					},
					error: function(e) {
						console.log("错误");
					}
				});
			},
			cont: function(d) { //当前界面加载店家信息
				let data = {};
				data.id = d;
				common_ajax("get_coupon_info", data, "", function(h) {
					app.content = h;
					app["shop"].shop_name = h.result.info.shop_name;
					app["shop"].shop_logo = h.result.info.shop_logo;
					app["shop"].name = h.result.info.name;
					app["shop"].list = h.result.shop_list;
					app["shop"].con = h.result.info.content;
					app["shop"].shop_endtime = h.result.info.end_time;
				});
			},
			mycont: function(f) { //领取优惠券成功后查看
				let code = {};
				code.id = f; //领取优惠券成功后返回id
				common_ajax("get_my_coupon_info", code, "", function(h) {
					if(h.status == "0") {
						alert("操作失败");
						return
					}
					app["shop"].shop_logo = h.result.info.shop_logo;
					app["shop"].shop_name = h.result.info.shop_name;
					app["shop"].name = h.result.info.name;
					app["shop"].list = h.result.shop_list;
					app["shop"].con = h.result.info.content;
					app["shop"].shop_endtime = h.result.info.end_time;
					//领取成功后开始使用
					let status = h.result.info.status;
					if(status == "1") { //生效
						$("#code").show();
						alert();
						app.sn = h.result.info.sn;
						codes(h.result.info.sn); //渲染二维码
					} else if(status == "2") { //已过期
						$("#lose-code").show();
					} else if(status == "3") { //次日生效
						app["shop"].shop_endtime = "次日生效";
						$("#code").show();
						codes("123"); //渲染二维码
						$("#cod").css({
							opacity: 0.2
						});
					}

				});
			}
		}
	})
	//首次加载语言
	if(localStorage.getItem("lanuage")) {
		app.Langes(localStorage.getItem("lanuage"));
	} else {
		app.Langes("en");
	}
	//获取url地址传参
	var receive = getQueryString("receive"); //领取优惠券所携带id
	var use = getQueryString("use"); //优惠券领取成功后id
	if(receive) { //输入手机号领取优惠券
		$("form").show();
		if(n == null) { //判断是否为新用户
			$("#phone").hide();
		} else {
			$("#phone").show();
		}
		//加载优惠券店家的详细信息
		app.cont(receive);
		//将优惠券所在店铺做缓存
		sessionStorage.setItem("receive", receive);
	} else if(use) { //领取成功以后展示信息
		app.mycont(use);
	}
	//领取优惠券
	$("form button").click(function() {
		if(n !== null) {
			if(!$("#phone").val()) return false;
		}
		let data = {},
			phone; //data:传输数据, phone:手机号
		phone = $("#phone").val();
		data.id = receive;
		data.mobile = phone;
		common_ajax("put_coupon_receive", data, "", function(h) {
			if(h.status == 1) { //回调函数为status1是成功
				sessionStorage.setItem("new", "1"); //此后用户将不再是新用户
				//$(window).attr('location', '../Payment/Payment.html?receive=' + receive);
				$("#succeed").show();
				app.coupon_id=h.result.coupon_id;
				sessionStorage.setItem("receive", h.result.coupon_id);
			} else if(h.status == 5) {
				alert("升级为会员后便可领取");
				$(window).attr("location", "../login/reg.html");
			} else if(h.status == 0) {
				if(localStorage.getItem("member_id") != null) {
					alert("您已经领取过优惠券了,可以到我的优惠券查看");
				} 
			}

		});
	})
	//二维码生成
	function codes(txt) {
		$("#cod").qrcode({
			render: "canvas", //canvas方式
			width: 100, //宽度
			height: 100, //高度
			text: txt //任意内容
		});
	}
	//var global_oss="http://foodietest.oss-cn-beijing.aliyuncs.com/"
	//var global_oss = "http://foodievc.oss-ap-southeast-1.aliyuncs.com/" //国外
	$("#l-ticket").click(function() {
		$(window).attr("location", "MyTicket.html");
	})
})